package com.framework.loippi.controller.api;

import com.framework.loippi.api.utils.ApiUtils;
import com.framework.loippi.entity.Pic;
import com.framework.loippi.entity.TMedicine;
import com.framework.loippi.service.PicService;
import com.framework.loippi.service.TMedicineService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller("apiMedicineController")
@Api(tags = {"药品信息"})
@RequestMapping(value = "/api/medicine")
public class MedicineController extends ApiBaseController {

    @Resource
    TMedicineService medicineService;
    @Resource
    PicService picService;

    @RequestMapping(value = "/findByType.json", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ApiOperation(value = "根据类型查询药品", notes = "{}")
    public @ResponseBody
    String findByType(long typeId) {
       List<TMedicine> list = medicineService.findByType(typeId);

       return ApiUtils.success(list);
    }

    @RequestMapping(value = "/findAll.json", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ApiOperation(value = "查询所有没被禁用的药品", notes = "{}")
    public @ResponseBody
    String findAll() {
        List<TMedicine> list = medicineService.findAllByStatus();

        return ApiUtils.success(list);
    }

    @RequestMapping(value = "/findById.json", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ApiOperation(value = "根据id查询药品详情", notes = "{}")
    public @ResponseBody
    String findById(Long id) {
        TMedicine medicine = medicineService.find(id);
        List<Pic> list = picService.findByMedicineId(id);
        List<String> images = new ArrayList<String>();
        for (Pic p : list) {
            images.add(p.getImgpath());
        }
        medicine.setImages(images);
        return ApiUtils.success(medicine);
    }


}
